//
//  DHComplicationsViewController.m
//  DHKuaiYi
//
//  Created by  chenhanmao on 2016/11/15.
//  Copyright © 2016年 30-陈辉华. All rights reserved.
//

#import "DHComplicationsViewController.h"
#import "DHComplicationsTableViewCell.h"
#import "DHComplicationsModel.h"
#import "NetworkTool.h"
#import <SVProgressHUD.h>
#import <YYModel.h>
#import <MJRefresh.h>

#define delay(time) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((time) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{[SVProgressHUD dismiss];})
#define delayUpdata(time) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((time) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{[self.tableView.mj_header endRefreshing];[self.tableView.mj_footer endRefreshing];})

static NSString *reuseCellID = @"reuseCellID";
@interface DHComplicationsViewController ()<UITableViewDelegate,UITableViewDataSource>

/**  并发症数据源  */
@property(nonatomic,strong) NSArray <DHComplicationsModel *> *complicationsList;

/**  tableview  */
@property(nonatomic,weak) UITableView *tableView;

/**  选中的并发症  */
@property(nonatomic,strong) NSMutableArray *selectedComplications;

@end

@implementation DHComplicationsViewController

#pragma mark - 网络请求

/**
 网络请求
 */
-(void)request {
    NSString *URLStr = @"http://iosapi.itcast.cn/doctor/complicationList.json.php";
    
    NSDictionary *parameters = @{@"page":@1,@"page_size":@15,@"ci1_id":@1,@"ci2_id":@3};
    
    [[NetworkTool sharedTool] POSTWithURLStr:URLStr parameters:parameters success:^(id responseObject) {
        if (responseObject != nil) {
            //调用第三方弹出提示框
            [SVProgressHUD showWithStatus:@"正在加载中...."];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            delay(1.0);
            //结束刷新
            delayUpdata(1.0);
            NSArray *dictArr = responseObject[@"data"];
            
            self.complicationsList = [NSArray yy_modelArrayWithClass:[DHComplicationsModel class] json:dictArr];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                
                [self.tableView reloadData];
            }];
        }
    } failed:^(NSError *error) {
        //调用第三方弹出提示框
        [SVProgressHUD showWithStatus:@"您的网络不给力,加载失败!"];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        delay(1.0);
        [self.tableView.mj_header endRefreshing];
        
    }];
}


#pragma mark - dataSource方法的实现

//  返回行数
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.complicationsList.count;
}

//  返回cell
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DHComplicationsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCellID];
    if (cell == nil) {
        cell = [[DHComplicationsTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    
    DHComplicationsModel *model = self.complicationsList[indexPath.row];
    
    cell.complications = model;
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //回调并发症类型
    DHComplicationsModel *model = self.complicationsList[indexPath.row];
    
    model.isSelected = !model.isSelected;
    
    DHComplicationsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.complications = model;
    
}




#pragma mark - 设置导航栏
- (void)xhhSetupNavigationItems {
    [super xhhSetupNavigationItems];
     self.title = @"选择并发症";
    
    UIBarButtonItem *finishBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finishedPickComplications)];
    
    self.navigationItem.rightBarButtonItem = finishBtn;
}

//选择完成后，回调选中数据
- (void)finishedPickComplications {
    
    //遍历查找选中状态
    [self.complicationsList enumerateObjectsUsingBlock:^(DHComplicationsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.isSelected == YES) {
            
            [self.selectedComplications addObject:obj.complication_name];
        }
    }];
    
    if (self.selectedComplications.count > 0) {
        
        self.complicationsBlcok(self.selectedComplications.copy);
    }
    
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 搭建界面
- (void)xhhSetupSubviews {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView = tableView;
    
    [self.view addSubview:tableView];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    tableView.backgroundColor = [UIColor XHH_colorWithHex: 0xefeff4];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self request];

    //实例化选中并发症
    self.selectedComplications = [NSMutableArray array];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
